CreateNewController = function(){
	
	
	this.initPageData = function(){	
		var transferParam = CommonUtil.takeOutParamObject();
		if(!CommonUtil.isNotBlank(transferParam)){
			dialogWarn("跳转页面参数异常!");
			return;
		}else{		
 		
		    this.initActivityPublicParam();	
			//组装其他设置页面的默认值
	        buildDefaultOtherSetting();
			//渲染页面动态数据;
			this.renderHtmlData();
			//绑定活动日期联动控件;
			this.bindActivityTimeWidget();				
		}
		
	};
	
	/**
	 * 绑定活动日期联动控件;	
	 */
	this.bindActivityTimeWidget = function(){				
		ActivityUtil.loadTimeWidget(true,function(){
			var activityDateParams = createActivityPublicParam.selectDateParams;
			activityDateParams.dateType = "activityDate";
			activityDateParams.pageId = "createNewActivityPage";
			activityDateParams.beginDateInputId = "act-date-start";
			activityDateParams.endDateInputId = "act-date-end";
			activityDateParams.selectBeginDateFunc = function(selectTime){
				if(selectTime != createActivityPublicParam.activityStartTime){
					createActivityPublicParam.activityStartTime = selectTime;
					createActivityPublicParam.activityEndTime = DateUtil.getDateFormat(DateUtil.addDays(DateUtil.str2Date(selectTime),30));
		        	$("#createNewActivityPage #act-date-end").val("");
		        	ActivityUtil.loadTimeWidget(true);
				}
			};
			activityDateParams.selectEndDateFunc = function(selectTime){
				if(selectTime != createActivityPublicParam.activityEndTime){
	            	createActivityPublicParam.activityEndTime = selectTime;
	            	ActivityUtil.loadTimeWidget(true);
				}		
			};
			
		});
	}
	
	/**
	 * 绑定活动日期联动控件;	
	 */
	this.bindVoucherTimeWidget = function(){	
		ActivityUtil.loadTimeWidget(true,function(){
			var activityDateParams = createActivityPublicParam.selectDateParams;
			activityDateParams.dateType = "voucherDate";
			activityDateParams.pageId = "createNewActivityPage";
			activityDateParams.beginDateInputId = "voucher-date-start";
			activityDateParams.endDateInputId = "voucher-date-end";
			activityDateParams.selectBeginDateFunc = function(selectTime){
				if(selectTime != createActivityPublicParam.voucherStartTime){
					createActivityPublicParam.voucherStartTime = selectTime;
					createActivityPublicParam.voucherEndTime = DateUtil.getDateFormat(DateUtil.addDays(DateUtil.str2Date(selectTime),30));
		        	$("#createNewActivityPage #voucher-date-end").val("");
		        	ActivityUtil.loadTimeWidget(true);
				}
			};
			activityDateParams.selectEndDateFunc = function(selectTime){
				if(selectTime != createActivityPublicParam.voucherEndTime){
	            	createActivityPublicParam.voucherEndTime = selectTime;
	            	ActivityUtil.loadTimeWidget(true);
				}		
			};
			
		});
	}
 
	
	
	
    /**
     * 页面首次加载初始化活动基础数据;
     */
	this.initActivityPublicParam = function(){
		var transferParam = CommonUtil.takeOutParamObject();
		var act = ActivityUtil.getActivityByType(transferParam.activityType);
		createActivityPublicParam.defaultParam = transferParam;
		createActivityPublicParam.activityType = transferParam.activityType;
		createActivityPublicParam.activityName = act.des+"活动"+DateUtil.getDateFormat(new Date());
		createActivityPublicParam.activityStartTime = DateUtil.getDateFormat(new Date());
		createActivityPublicParam.activityEndTime = DateUtil.getDateFormat(DateUtil.addDays(new Date(),30));
        var data = createActivityPublicParam.prerequisiteData;
        createActivityPublicParam.voucherLogo = data.brandLogoId;
        createActivityPublicParam.voucherLogoUri = data.brandLogoUrl;
        createActivityPublicParam.voucherSubTitle =  (data.brandName.length>20)?data.brandName.slice(0,20):data.brandName;
        if(createActivityPublicParam.activityType != ACTIVITY_TYPE.ITEM_MARKET.val){
        	  createActivityPublicParam.voucherWorthValue = parseFloat(data.customerPrice * 0.1).toFixed(2);
              if(createActivityPublicParam.voucherWorthValue < 1){
              	createActivityPublicParam.voucherWorthValue = "1.00";
              }
              createActivityPublicParam.voucherMinConsume = parseFloat(data.customerPrice).toFixed(2);
              createActivityPublicParam.promoSendRuleMincost = parseFloat(data.customerPrice * 0.8).toFixed(2);
              createActivityPublicParam.voucherRelativeTime = 30;
              createActivityPublicParam.voucherStartTime = createActivityPublicParam.activityStartTime;
              createActivityPublicParam.voucherEndTime = createActivityPublicParam.activityEndTime;
        }   
        createActivityPublicParam.brandName = data.brandName; 
        if(createActivityPublicParam.voucherData && createActivityPublicParam.voucherData.length>0){
        	for(var i=0;i<createActivityPublicParam.voucherData.length;i++){
    			createActivityPublicParam.voucherData[i].voucherName = createActivityPublicParam.voucherData[i].voucherWorthValue
    					+ createActivityPublicParam.brandName + "全场代金券";// 券面额＋品牌名称＋券类型;
    			createActivityPublicParam.voucherData[i].voucherLogoUri =  createActivityPublicParam.voucherLogoUri;
    			createActivityPublicParam.voucherData[i].voucherLogo = createActivityPublicParam.voucherLogo;
     		}
        }   
     
        buildDefaultOtherSetting();       
	}
	/**
	 * 初始化编辑代金券页面数据;
	 */
	this.initEditVoucherPageData = function(){
		createActivityPublicParam.voucherStartTime = (createActivityPublicParam.voucherStartTime=="")?createActivityPublicParam.activityStartTime:createActivityPublicParam.voucherStartTime;
        createActivityPublicParam.voucherEndTime = (createActivityPublicParam.voucherEndTime=="")?createActivityPublicParam.activityEndTime:createActivityPublicParam.voucherEndTime;
		$("#createNewActivityPage #voucherAmount").val(createActivityPublicParam.voucherWorthValue);
		$("#createNewActivityPage #voucherMinConsumeAmount").val(createActivityPublicParam.voucherMinConsume);
		$("#createNewActivityPage #voucher-date-start").val(createActivityPublicParam.voucherStartTime);
		$("#createNewActivityPage #voucher-date-end").val(createActivityPublicParam.voucherEndTime);	
		if(createActivityPublicParam.isRelativeTime == true){
			$("#createNewActivityPage #quan-deadline").find("span").text("相对时间");
			$("#createNewActivityPage #relativeValidDays").val(createActivityPublicParam.voucherRelativeTime);
			$("#createNewActivityPage #quan-deadline.switch-label").siblings("p").hide().eq(0).show();	 
		}else{
			$("#createNewActivityPage #quan-deadline").find("span").text("指定时间");
			$("#createNewActivityPage #quan-deadline.switch-label").siblings("p").hide().eq(1).show();		 		
		}
	}
	/**
	 * 保存券数据;
	 */
	this.saveVoucherData = function(){
		var otherSetting = createActivityPublicParam.otherSetting;
		if($("#createNewActivityPage #quan-deadline").find("span").text()=="相对时间"){
			createActivityPublicParam.isRelativeTime = true;
			createActivityPublicParam.voucherRelativeTime = $("#createNewActivityPage #relativeValidDays").val();
			otherSetting.voucherRelativeTime=createActivityPublicParam.voucherRelativeTime;
		}else{
			createActivityPublicParam.isRelativeTime = false;
			createActivityPublicParam.voucherStartTime = $("#createNewActivityPage #voucher-date-start").val();
			createActivityPublicParam.voucherEndTime = $("#createNewActivityPage #voucher-date-end").val();
		}
		createActivityPublicParam.voucherWorthValue =  parseFloat($("#createNewActivityPage #voucherAmount").val()).toFixed(2);
		createActivityPublicParam.voucherMinConsume = parseFloat($("#createNewActivityPage #voucherMinConsumeAmount").val()).toFixed(2);    
        otherSetting.voucherWorthValue = createActivityPublicParam.voucherWorthValue;
	    otherSetting.voucherMinConsume = createActivityPublicParam.voucherMinConsume;
	    otherSetting.voucherName = buildVourcherName(otherSetting.voucherWorthValue);
	    otherSetting.voucherValidateType =  createActivityPublicParam.isRelativeTime?"RELATIVE":"FIXED";
	 			
	} 
	
	/**
	 * 保存分佣数据;
	 */
	this.saveCommissionData = function(){
		var isOpenKbk = false;
		var openKbkDesc = $("#createNewActivityPage #kbk_desc").text();	
		if(openKbkDesc.indexOf("关闭")!=-1){
			isOpenKbk = true;	
			if(createActivityPublicParam.activityType == ACTIVITY_TYPE.ITEM_MARKET.val){
				createActivityPublicParam.quotaClauseQuotaAmount = $("#createNewActivityPage #commissionAmount").val();	
			}else{
				if($("#createNewActivityPage #commission-deadline").find("span").text()=="限制"){
		    		createActivityPublicParam.commissionSettingCheck = "1";
		    		createActivityPublicParam.percentageClauseMax = $("#createNewActivityPage #commissionLimit").val();
		    	}else{
		    		createActivityPublicParam.commissionSettingCheck = "0";    		
		    	}
		    	createActivityPublicParam.percentageClauseRate = $("#createNewActivityPage #commissionRate").val();
			}				
		} 		
		createActivityPublicParam.isOpenKbk = isOpenKbk;
	}
	/**
	 * 初始化佣金页面数据;
	 */
	this.initCommissionPageData = function(){
		if (createActivityPublicParam.activityType == ACTIVITY_TYPE.ITEM_MARKET.val) {// 单品营销,只显示分佣金额;
			 $("#df-koubeike .form-wrapper>ul").find("li").each(function(){  
				 if ($(this).attr("class") != null && $(this).attr("class").indexOf("ischange") != -1) {
						if ($(this).attr("id") != null && $(this).attr("id") == "commissionAmountLi") {
							$(this).removeClass("hide");
						} else {
							$(this).addClass("hide");
						}
			     }
			 });		
 		}else{//非单品营销，不显示分佣金额;
			$("#commissionAmountLi").addClass("hide");
			$("#commissionRateLi").removeClass("hide");
			$("#commissionLimitLi").removeClass("hide");
		}		
		if(createActivityPublicParam.isOpenKbk==true){
			this.openKoubeiKe();
			if(createActivityPublicParam.activityType == ACTIVITY_TYPE.ITEM_MARKET.val){
				$("#commissionAmount").val(createActivityPublicParam.quotaClauseQuotaAmount);
			}else{
				if(createActivityPublicParam.commissionSettingCheck == "1"){
					$("#commission-deadline").find("span").text("限制");
					$("#commission-deadline.switch-label").siblings("p").show();
					$("#commissionLimit").val(createActivityPublicParam.percentageClauseMax);
				}else{
					$("#commission-deadline").find("span").text("不限制");
					$("#commission-deadline.switch-label").siblings("p").hide();
				}
				$("#commissionRate").val(createActivityPublicParam.percentageClauseRate);
			}
			
		}else{
			this.closeKoubeiKe();
			if(createActivityPublicParam.activityType != ACTIVITY_TYPE.ITEM_MARKET.val){//非单品营销活动;			 
				$("#commissionRate").val("");
				$("#commission-deadline").find("span").text("不限制");
				$("#commission-deadline.switch-label").siblings("p").hide();
			}else{			 
				$("#commissionAmount").val("");
			}		
		}	
		
	}
	
	/**
	 * 开启口碑客元素显示;
	 */
	this.openKoubeiKe = function(){
		 $("#df-koubeike .form-wrapper>ul").find("li").each(function(){  
   		  if($(this).attr("class")!=null && $(this).attr("class").indexOf("ischange")!=-1){
   			  $(this).removeClass("disabled");  
   			  $(this).find("div").find("input").removeAttr("readonly");
   		  }       	          
         }); 
   	     $("#kbk_desc").text("关闭口碑客");
   	     createActivityPublicParam.isOpenKbk = true;
	}
	/**
	 * 关闭口碑客元素显示;
	 */
	this.closeKoubeiKe = function(){
		  $("#df-koubeike .form-wrapper>ul").find("li").each(function(){     
        	 if($(this).attr("class")!=null && $(this).attr("class").indexOf("ischange")!=-1){
           	     $(this).addClass("disabled");   
           	     $(this).find("div").find("input").attr("readonly","readonly");
        	 }
          });
          $("#kbk_desc").text("开启口碑客"); 
          createActivityPublicParam.isOpenKbk = false;
	}
	
	/**
	 * 初始化支付宝专享优惠页面数据;
	 */
	this.initZfbPageData = function(){
		$("#createNewActivityPage #discountDoc").val(createActivityPublicParam.voucherSlogan); 
		var picElt = "";
		var voucherSloganPicUrl = createActivityPublicParam.voucherSloganLocalFilePath;
	    if(voucherSloganPicUrl!=""){
	    	picElt = "<img src='"+voucherSloganPicUrl+"' alt='优惠专属图片'/><i class='zhk-list_rarrow'></i>";    	
	    }else{
	    	picElt = "非必选项<i class='zhk-list_rarrow'></i>";
	    }
	    $("#createNewActivityPage #discountPic").find("a").html(picElt);

	}
	
	/**
	 * 保存支付宝专享优惠页面数据;
	 */
	this.saveZfbData = function(){
		createActivityPublicParam.voucherSlogan =$("#createNewActivityPage #discountDoc").val();//专属优惠文案	
		createActivityPublicParam.voucherSloganLocalOldFilePath = createActivityPublicParam.voucherSloganLocalFilePath;//专属优惠图片本地文件path,新路径和老路径进行同步;
		createActivityPublicParam.voucherSloganUri = createActivityPublicParam.voucherSloganNewUri;//保存专享优惠新图片url;
		createActivityPublicParam.voucherSloganUrlId = createActivityPublicParam.voucherSloganNewId;//保存专享优惠新图片id;
	}
 
	
	/**
	 * 渲染页面显示;
	 */
	this.renderHtmlData = function(){
		//渲染模版动态数据;
        CommonUtil.render($("#createNewActivityPage #activityDataContainer"), "activity_base_data_tpl",createActivityPublicParam);	
    	//根据活动初始化页面标签显示逻辑;
        ActivityUtil.controlPageLabel("createNewActivityPage",createActivityPublicParam.activityType);
    	       
	}

	/**
	 * 检查代金券信息;
	 */
	this.checkVoucherData = function(){
		var voucherAmount = $("#createNewActivityPage #voucherAmount").val();
		if (voucherAmount == "") {
			DialogUtil.dialogWarn("请您填写券面额!");
			return false;
		}
		if(isNaN(voucherAmount)){
			DialogUtil.dialogWarn('券面额仅允许输入数字');
			return false;
		}
		if (voucherAmount <= 0 || voucherAmount > 999.99) {
			DialogUtil.dialogWarn("券面额必须输入大于0且小于999.99的数字!");
			return false;
		}
		var voucherMinConsumeAmount = $("#createNewActivityPage #voucherMinConsumeAmount").val();
		if (voucherMinConsumeAmount == "") {
			DialogUtil.dialogWarn("请填写券消费门槛!");
			return false;
		}
		if(isNaN(voucherMinConsumeAmount)){
			DialogUtil.dialogWarn('券消费门槛金额仅允许输入数字');
			return false;
		}
 
		if (voucherMinConsumeAmount < 0 || voucherMinConsumeAmount > 9999.99) {
			DialogUtil.dialogWarn("券消费门槛金额必须输入大于等于0且小于9999.99的数字!");
			return false;
		}
		if($("#createNewActivityPage #quan-deadline").find("span").text()=="相对时间"){
			var relativeDays = $("#createNewActivityPage #relativeValidDays").val();
			if (relativeDays <= 0 || relativeDays == "") {
				DialogUtil.dialogWarn("券有效期相对时间最小为1天");
				return false;
			}
			if (!(/^\+?(0|[1-9][0-9]*)$/.test(relativeDays))) {
				DialogUtil.dialogWarn('券有效期相对时间允许输入整天数');
				return false;
			}
			if (relativeDays > 365) {
				DialogUtil.dialogWarn("券有效期相对时间不能大于365天!");
				return false;
			}
		}else{
			var voucherEndDate = $("#createNewActivityPage #voucher-date-end").val();
			if(voucherEndDate == ""){
				DialogUtil.dialogWarn("请选择券的有效结束日期!");
				return false;
			}
			
		}
	
		return true;
	}
	
	/**
	 * 检查口碑客数据;
	 */
	this.checkKbKData = function(){
	  if(createActivityPublicParam.isOpenKbk == true){
		    var kbkConfigInfo = createActivityPublicParam.kbkConfigInfo;
		    if(createActivityPublicParam.activityType == ACTIVITY_TYPE.ITEM_MARKET.val){
		    	var commissionAmount = $("#createNewActivityPage #commissionAmount").val();
		    	if(commissionAmount == ""){
		    		 DialogUtil.dialogWarn("请填写分佣金额!");
		    		 return false;
		    	}
		    	if(isNaN(commissionAmount)){
					 DialogUtil.dialogWarn("分佣金额仅允许输入数字!");
					 return false;
				}
		    	if(Number(commissionAmount)< Number(kbkConfigInfo.minQuotaAmount)){
		    		 DialogUtil.dialogWarn("分佣金额不能小于"+kbkConfigInfo.minQuotaAmount+"元");
		    		 return false;
		    	}
		    	if(Number(commissionAmount)> Number(kbkConfigInfo.maxQuotaAmount)){
		    		 DialogUtil.dialogWarn("分佣金额不能大于"+kbkConfigInfo.maxQuotaAmount+"元");
		    		 return false;
		    	}
		    	if(Number(commissionAmount) > Number(createActivityPublicParam.voucherWorthValue)){
		    		 DialogUtil.dialogWarn("分佣金额不能大于券面额");
		    		 return false;
		    	}
		        return true;
		    }
			  
		    var commssionRate = $("#createNewActivityPage #commissionRate").val();
			if(commssionRate == ""){
				  DialogUtil.dialogWarn("请填写佣金比例!");
				  return false;
			}
			if(isNaN(commssionRate)){
				 DialogUtil.dialogWarn("分佣比例仅允许输入数字!");
				 return false;
			}
			if(Number(commssionRate) < Number(kbkConfigInfo.minCommissionPercentage)){
				 DialogUtil.dialogWarn("分佣比例不能低于"+kbkConfigInfo.minCommissionPercentage+"%");
				 return false;
			}
			if(Number(commssionRate)>99.99){
				DialogUtil.dialogWarn("分佣比例不能超过99.99%");
				return false;
			}
			
			if($("#createNewActivityPage #commission-deadline").find("span").text()=="限制"){
				var commissionLimit = $("#createNewActivityPage #commissionLimit").val();
				if(commissionLimit == ""){
					  DialogUtil.dialogWarn("请填写分佣上限!");
					  return false;
				}
				if(isNaN(commissionLimit)){
					DialogUtil.dialogWarn("分佣上限仅允许输入数字!");
					return false;
				}
				if(commissionLimit < 0){
					DialogUtil.dialogWarn("分佣上限不能小于0");
					return false;
				}
				if(Number(commissionLimit) > Number(kbkConfigInfo.maxMaxAmount)){
					DialogUtil.dialogWarn("分佣上限不能超过"+ kbkConfigInfo.maxMaxAmount);
	 			    return false;
				}
	 
			}
	  }	
 
      return true;
		
	}
    /**
     * 保存创建活动页面可能变化的数据;
     */
	this.saveActivityData = function(){
		ActivityPreloadData.initActivityCheckedChannel("createNewActivityPage");
		if($("#createNewActivityPage #auto_continue").prop("checked")){
		    createActivityPublicParam.autoDelayFlag = "Y";
		}else{
			createActivityPublicParam.autoDelayFlag = "N";
		}
		if(createActivityPublicParam.affiliation!=""){//如果二维码类型归属不为空,则将QR_CODE放入渠道;
			createActivityPublicParam.channelType.push("QR_CODE");
		}
	}
	
	
};